package com.lg.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.retry.RetryNTimes;

public class CuratorWatcherTest {
	private static final String ZK_PATH = "/zktest";

	public static void main(String[] args) throws Exception {
		CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new RetryNTimes(10, 5000));
		client.start();
		System.out.println("zk client start successfully!");
		PathChildrenCache watcher = new PathChildrenCache(client, ZK_PATH, true);
		watcher.getListenable().addListener((curatorFramework, event) -> {
			ChildData data = event.getData();
			if (data == null) {
				System.out.println("No data in event[" + event + "]");
			} else {
				System.out.println("Receive event: " + "type=[" + event.getType() + "]" + ", path=[" + data.getPath()
						+ "]" + ", data=[" + new String(data.getData()) + "]" + ", stat=[" + data.getStat() + "]");
			}
		});
		watcher.start(StartMode.BUILD_INITIAL_CACHE);
		System.out.println("Register zk watcher successfully!");

		Thread.sleep(Integer.MAX_VALUE);
	}
}
